package system.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import system.common.Constants;
import system.model.DB10_iDeskDto;
import system.model.DB1_WebkeeperDto;
import system.model.DB2_MsgIDto;
import system.model.DB3_DbIDto;
import system.model.DB4_PrivacyIDto;
import system.model.DB5_NetKeyDto;
import system.model.DB6_RedcastleDto;
import system.model.DB7_PowerBrokerDto;
import system.model.DB8_TxLoggingDto;
import system.model.DB9_PortalLoginDto;
import system.util.FileUtil;

public class ExportService {
	@SuppressWarnings({ "unchecked" })
	public void OutputList(List<?> dtoRec, Writer writer, String historyFileName, String stepNumber, int fileCount, String cmd) throws IOException {
		long currentFileSize = 0;
		File file = null;
		String fileNumber = FileUtil.genFileNumber(fileCount);
		
		if (dtoRec.size() > 0) {
			Object o = dtoRec.get(0);
			if (o instanceof DB1_WebkeeperDto) {
				for (DB1_WebkeeperDto record : (List<DB1_WebkeeperDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					if (cmd.equals("WEBKEEPER_01")) {
						writer.write(record.toString());	
					} else if (cmd.equals("WEBKEEPER_02")) {
						writer.write(record.toString());
					} else if (cmd.equals("WEBKEEPER_MIGRATE_01")) {
						writer.write(record.toString());
					} else if (cmd.equals("WEBKEEPER_MIGRATE_02")) {
						writer.write(record.toString());
					}
				}
			} else if (o instanceof DB2_MsgIDto) {
				for (DB2_MsgIDto record : (List<DB2_MsgIDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					if (cmd.equals("MSG_I_01")) {
						writer.write(record.toStringMail());	
					} else if (cmd.equals("MSG_I_02")) {
						writer.write(record.toStringFile());
					} else if (cmd.equals("MSG_I_03")) {
						writer.write(record.toStringBlock());
					}
				}
			} else if (o instanceof DB3_DbIDto) {
				for (DB3_DbIDto record : (List<DB3_DbIDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			} else if (o instanceof DB4_PrivacyIDto) {
				for (DB4_PrivacyIDto record : (List<DB4_PrivacyIDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			} else if (o instanceof DB5_NetKeyDto) {
				for (DB5_NetKeyDto record : (List<DB5_NetKeyDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			} else if (o instanceof DB6_RedcastleDto) {
				for (DB6_RedcastleDto record : (List<DB6_RedcastleDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					if (cmd.equals("REDCASTLE_01")) {
						writer.write(record.toStringLoginlog());
					} else if (cmd.equals("REDCASTLE_03")) {
						writer.write(record.toStringFtplog());
					} else if (cmd.equals("REDCASTLE_03")) {
						writer.write(record.toStringSessionlog());
					}
				}
			} else if (o instanceof DB7_PowerBrokerDto) {
				for (DB7_PowerBrokerDto record : (List<DB7_PowerBrokerDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					if (cmd.equals("POWERBROKER_01")) {
						writer.write(record.toStringInput());
					} else if (cmd.equals("POWERBROKER_02")) {
						writer.write(record.toStringOutput());
					}
				}
			} else if (o instanceof DB8_TxLoggingDto) {
				for (DB8_TxLoggingDto record : (List<DB8_TxLoggingDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			} else if (o instanceof DB9_PortalLoginDto) {
				for (DB9_PortalLoginDto record : (List<DB9_PortalLoginDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			} else if (o instanceof DB10_iDeskDto) {
				for (DB10_iDeskDto record : (List<DB10_iDeskDto>) dtoRec) {
					currentFileSize = currentFileSize + record.toString().getBytes().length;
					if (currentFileSize >= Constants.maxFileSize) {
						writer.flush();
						writer.close();

						currentFileSize = 0;
						fileCount++;
						fileNumber = FileUtil.genFileNumber(fileCount);
						file = new File(historyFileName + stepNumber + fileNumber + Constants.fileNameExtension);
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
					}
					writer.write(record.toString());
				}
			}
		}
		writer.flush();
		writer.close();
	}
}